package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.dao.ISearchConDicRelaDao;
import com.magicode.cms.entiy.base.SearchConDicRela;
import com.magicode.cms.service.ISearchConDicRelaService;

@Service("serviceSearchConDicRela")
public class SearchConDicRelaServiceImpl extends BaseServiceImpl<SearchConDicRela,ISearchConDicRelaDao>  implements ISearchConDicRelaService{
	@Autowired
	@Qualifier("searchConDicRelaDao")
	private ISearchConDicRelaDao searchConDicRelaDao;
	
	@Override
	public String getHql(Map<String, String> map) throws Exception {
		String hql ="from " +SearchConDicRela.class.getName()+" "+entity_alias_name+" "
//				+ " left join t.country as c "
//				+ " left join t.city as ct "
				+ " left join t.dictionary as d "
				+ " left join t.searchCondition as s "
				+ " where 1 = 1 ";
		if(null!=map && !map.isEmpty()){
			if(map.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id = '"+map.get(field_id)+"'";
			}
		}	
		return hql;
	}


	
	@Override
	public List<Map<String,Object>> select(Map<String,String> querywhere){
		try {
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			return this.queryMapDataList(hql, gridsel,0 , 0);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}
	@Override
	public SearchConDicRela getSourceByTitle(String name) throws Exception {
		try {
			Map<String,String> map = new HashMap<String,String> ();
			map.put("title", name);
			List<SearchConDicRela> sources = this.getDao().queryDataList(this.getHql(map), 0, 0);
			return  sources.size()==1?sources.get(0) : null;
		} catch (Exception e) {
			//throw new Exception("getSourceByTitle error by email=["+name+"]"+e.getMessage());
			throw new Exception("getSourceByTitle error by title=["+name+"]"+e.getMessage());
		}
	}
	@Override
	public void deleteSearchConDicRela(String userid) {
		// TODO Auto-generated method stub
		String hql = " from "+SearchConDicRela.class.getName()+" t " +
				"  where  t.id = '"+userid+"'" ;
		List<SearchConDicRela> vphs = this.searchConDicRelaDao.getCommonDaoService().getListByHQL(hql);
		this.searchConDicRelaDao.getCommonDaoService().deleteAll(vphs);
	}
	
	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map = new HashMap<String, Object>();
		map.put(json_key, "searchCondition");
		map.put(json_title, "关联查询条件表");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "orderCode");
		map.put(json_title, "排序码");
		map.put("width", 50);
		beaninfo.add(map);
		
		return beaninfo;
	}

	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
			
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere,0, field_grid_size);
			String hql = this.getHql(querywhere);
			hql += "  order by "+entity_alias_name+".orderCode asc "; 
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put("searchCondition", " s.title as searchCondition ");
			gridsel.put("orderCode", ""+entity_alias_name+".orderCode as orderCode ");
			return this.queryMapDataList(hql, gridsel,start , size);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
		
	}


	@Override
	public List<Map<String,Object>> simpleSelect(Map<String,String> querywhere){
		try {
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put("names", "d.title   as text ");//|| c.title || ct.title
			return this.queryMapDataList(hql, gridsel,0 , 0);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}
	
	@Override
	public ISearchConDicRelaDao getDao() throws Exception {
		// TODO Auto-generated method stub
		return searchConDicRelaDao;
	}


	
}
